package com.example.service.impl;

import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.dao.SuppliesMapper;
import com.example.dao.SuppliesPropMapper;
import com.example.dao.SuppliesTypeMapper;
import com.example.model.entity.Supplies;
import com.example.model.entity.SuppliesProp;
import com.example.model.entity.SuppliesType;
import com.example.model.query.SuppliesTypeQuery;
import com.example.model.vo.SuppliesTypeVo;
import com.example.model.vo.ThresholdVo;
import com.example.service.SuppliesTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.common.base.service.impl.BaseServiceImpl;

@Service
@Slf4j
public class SuppliesTypeServiceImpl extends BaseServiceImpl<SuppliesTypeMapper, SuppliesType> implements SuppliesTypeService {


	@Autowired
	private SuppliesTypeMapper typeMapper;

	@Autowired
	private SuppliesPropMapper propMapper;

	@Autowired
	private SuppliesMapper suppliesMapper;

	@Override
	public ApiResult<SuppliesTypeQuery> pageUserVoByQuery(SuppliesTypeQuery query) {
		query.setOptimizeCountSql(false);
		Long current = query.getCurrent();
		query.setCurrent(1L);
		QueryWrapper<SuppliesType> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(SuppliesType::getIsValid,1L);
		Integer total = typeMapper.selectCount(queryWrapper);
		query.setTotal(total);
		query.setCurrent(current);
		typeMapper.selectPage(query,queryWrapper);
		return new ApiResult<>(query);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public ApiResult<Boolean> saveAd(SuppliesTypeVo suppliesTypeVo) throws Exception {
		QueryWrapper<SuppliesType> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(SuppliesType::getTypeNum,suppliesTypeVo.getTypeNum())
				.eq(SuppliesType::getIsValid,1L);
		int num = typeMapper.selectCount(queryWrapper);
		if(num > 0){
			return new ApiResult<>(Boolean.FALSE, ResponseCodeEnum.MALL_SERVICE_SAVE_TYPE_FALSE);
		}
		// 新增
		SuppliesType suppliesType = new SuppliesType();
		suppliesType.setTypeName(suppliesTypeVo.getTypeName());
		suppliesType.setCreateTime(new Date());
		suppliesType.setUpdateTime(new Date());
//		String num = this.seqAlways().toString();
//		suppliesType.setTypeId(num);
		suppliesType.setTypeNum(suppliesTypeVo.getTypeNum());
		//阈值处理
		if(suppliesTypeVo.getThresholdList()!=null && suppliesTypeVo.getThresholdList().size()>0){
			String str = JSON.toJSONString(suppliesTypeVo.getThresholdList());
			suppliesType.setThreshold(str);
		}
		this.save(suppliesType);
		for(SuppliesProp prop:suppliesTypeVo.getPropList()){
			prop.setTypeId(suppliesType.getTypeId());
			prop.setCreateTime(new Date());
			propMapper.insert(prop);
		}

		return new ApiResult<>(Boolean.TRUE);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public ApiResult<Boolean> update(SuppliesTypeVo suppliesTypeVo) throws Exception {
		QueryWrapper<SuppliesType> typeQueryWrapper = new QueryWrapper<>();
		typeQueryWrapper.lambda().eq(SuppliesType::getTypeNum,suppliesTypeVo.getTypeNum())
				.eq(SuppliesType::getIsValid,1L)
				.ne(SuppliesType::getTypeId,suppliesTypeVo.getTypeId());
		int num = typeMapper.selectCount(typeQueryWrapper);
		if(num > 0){
			return new ApiResult<>(Boolean.FALSE, ResponseCodeEnum.MALL_SERVICE_SAVE_TYPE_FALSE);
		}
		SuppliesType suppliesType = new SuppliesType();
		BeanUtils.copyProperties(suppliesTypeVo,suppliesType);
		//c处理阈值
		if(suppliesTypeVo.getThresholdList() != null && suppliesTypeVo.getThresholdList().size() > 0){
			String str = JSON.toJSONString(suppliesTypeVo.getThresholdList());
			suppliesType.setThreshold(str);
		}else{
			suppliesType.setThreshold("");
		}
		this.updateById(suppliesType);
		QueryWrapper<SuppliesProp> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(SuppliesProp::getTypeId,suppliesTypeVo.getTypeId())
				.eq(SuppliesProp::getIsValid,1L);
		List<SuppliesProp> datePropList = propMapper.selectList(queryWrapper);
		for(SuppliesProp prop : datePropList){
			prop.setIsValid(0L);
			propMapper.updateById(prop);
		}
		for(SuppliesProp prop:suppliesTypeVo.getPropList()){
			prop.setPropId(null);
			prop.setTypeId(suppliesTypeVo.getTypeId());
			prop.setCreateTime(new Date());
			propMapper.insert(prop);
		}
		return new ApiResult<>(Boolean.TRUE);
	}

	@Override
	public ApiResult<SuppliesTypeVo> getInfo(String id) throws Exception {
		SuppliesTypeVo vo = new SuppliesTypeVo();
		SuppliesType suppliesType = this.getById(id);
		BeanUtils.copyProperties(suppliesType,vo);
		QueryWrapper<SuppliesProp> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(SuppliesProp::getTypeId,id)
				.eq(SuppliesProp::getIsValid,1L);
		List<SuppliesProp> propList = propMapper.selectList(queryWrapper);
		vo.setPropList(propList);
		if(StringUtils.isNotBlank(vo.getThreshold())){
			//String转json，再转实体类
//			JSONObject json=JSON.parseObject(vo.getThreshold());
			//String转实体类
			List<ThresholdVo> thresholdList = JSON.parseArray(vo.getThreshold(), ThresholdVo.class);
			vo.setThresholdList(thresholdList);
		}
		return new ApiResult<>(vo);
	}

	/**
	 * 序列
	 */
	@Override
	public Long seqAlways(){
		return typeMapper.seqAlways();
	}


	@Override
	public ApiResult<Boolean> del(String id) throws Exception {
		SuppliesType suppliesType = typeMapper.selectById(id);
		if(suppliesType != null){
			suppliesType.setIsValid(0L);
			typeMapper.updateById(suppliesType);
		}
		return new ApiResult<>(Boolean.TRUE);
	}

	@Override
	public String getInfoBySuppliesId(String suppliesNum) throws Exception {
		QueryWrapper<Supplies> queryWrapper = new QueryWrapper();
		queryWrapper.lambda().eq(Supplies::getSuppliesNum,suppliesNum);
		Supplies supplies = suppliesMapper.selectOne(queryWrapper);
		if(supplies == null || StringUtils.isBlank(supplies.getTypeId())){
			return "";
		}
		SuppliesType suppliesType = this.getById(supplies.getTypeId());
		if(suppliesType == null  ){
			return "";
		}
		if(StringUtils.isBlank(suppliesType.getThreshold())){
			return "";
		}
		JSONObject JsonObject = new JSONObject().parseObject("{}");
		List<ThresholdVo> thresholdVoList = JSON.parseArray(suppliesType.getThreshold(), ThresholdVo.class);
		for(ThresholdVo vo:thresholdVoList){
			JsonObject.put(vo.getLabel(),vo.getValue());
		}
		String str = JSON.toJSONString(JsonObject);
		return str;
	}

}
